package com.zkthink.ceres.app.order.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.zkthink.base.R;
import com.zkthink.ceres.order.dto.*;
import com.zkthink.ceres.order.enums.ReturnGoodsReasonEnum;
import com.zkthink.ceres.order.enums.ReturnMoneyReasonEnum;
import com.zkthink.ceres.order.service.OrderService;
import com.zkthink.ceres.order.service.ReturnInterventionService;
import com.zkthink.ceres.order.vo.*;
import com.zkthink.ceres.product.dto.IdQueryDTO;
import com.zkthink.ceres.product.dto.PageQueryDTO;
import com.zkthink.context.BaseContextHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("/order")
@Api(value = "订单接口", tags = "订单接口")
@AllArgsConstructor
@Transactional
public class OrderController {

    private OrderService orderService;

    private ReturnInterventionService returnInterventionService;

    @ApiOperation("下单")
    @PostMapping("/placeOrder")
    public R<PlaceOrderVO> placeOrder(HttpServletRequest request, @RequestBody PlaceOrderDTO placeOrderDTO) {
        Long mid = BaseContextHandler.getUserId();
        placeOrderDTO.setMid(mid);
        String ip = ServletUtil.getClientIP(request);
        placeOrderDTO.setIp(ip);
        return orderService.placeOrder(placeOrderDTO);
    }


    @ApiOperation("快递订阅回调接口")
    @GetMapping("/expressSubscriptionCallbackInterface")
    public void expressSubscriptionCallbackInterface(HttpServletRequest request) {
        orderService.expressSubscriptionCallbackInterface(request);
    }


    @ApiOperation("支付")
    @GetMapping("/payOrder/{orderCode}")
    public R<Object> payOrder(HttpServletRequest request, @PathVariable("orderCode") String orderCode) {
        Long mid = BaseContextHandler.getUserId();
        return orderService.payOrder(mid, orderCode, ServletUtil.getClientIP(request));
    }

    @ApiOperation("继续付款")
    @PostMapping("/continuePay")
    public R<CountinuePayVo> continuePay(HttpServletRequest request, @RequestBody ContinuePayOrderDTO continuePayOrderDTO) {
        Long mid = BaseContextHandler.getUserId();
        continuePayOrderDTO.setMid(mid);

        String ip = ServletUtil.getClientIP(request);
        continuePayOrderDTO.setIp(ip);
        return orderService.continuePay(continuePayOrderDTO);
    }

    @ApiOperation("查询订单列表")
    @PostMapping("/findOrderList")
    public R<OrderListVO> findOrderList(@RequestBody FindOrderListDTO findOrderListDTO) {
        Long mid = BaseContextHandler.getUserId();
        findOrderListDTO.setMid(mid);
        return orderService.findOrderList(findOrderListDTO);
    }

    @ApiOperation("查询分销订单列表")
    @PostMapping("/findSalesOrderList")
    public R<OrderListVO> findSalesOrderList(@RequestBody FindOrderListDTO findOrderListDTO) {
        Long mid = BaseContextHandler.getUserId();
        findOrderListDTO.setMid(mid);
        return orderService.findSalesOrderList(findOrderListDTO);
    }

    @ApiOperation("申请退款")
    @PostMapping("/applyReturnMoney")
    public R<MoneyReturnVO> applyReturnMoney(@RequestBody ApplyReturnMoneyDTO applyReturnMoneyDTO) {
        Long mid = BaseContextHandler.getUserId();
        applyReturnMoneyDTO.setMid(mid);
        return orderService.applyReturnMoney(applyReturnMoneyDTO);
    }

    @ApiOperation("申请退货")
    @PostMapping("/applyReturnGoods")
    public R<GoodsReturnVO> applyReturnGoods(@RequestBody ApplyReturnGoodsDTO applyReturnGoodsDTO) {
        Long mid = BaseContextHandler.getUserId();
        applyReturnGoodsDTO.setMid(mid);
        return orderService.applyReturnGoods(applyReturnGoodsDTO);
    }

    @ApiOperation("申请平台介入")
    @PostMapping("/applyIntervention")
    public R<Boolean> applyIntervention(@RequestBody ApplyInterventionDTO applyInterventionDTO) {
        return returnInterventionService.applyIntervention(applyInterventionDTO);
    }

    @ApiOperation("填写退货物流")
    @PostMapping("/sendReturnGoods")
    public R<Boolean> sendReturnGoods(@RequestBody SendReturnGoodsDTO sendReturnGoodsDTO) {
        Long mid = BaseContextHandler.getUserId();
        sendReturnGoodsDTO.setMid(mid);
        return orderService.sendReturnGoods(sendReturnGoodsDTO);
    }

    @ApiOperation("结算")
    @PostMapping("/settlement")
    public R<SettlementVO> settlement(@RequestBody SettlementDTO settlementDTO) {
        Long mid = BaseContextHandler.getUserId();
        settlementDTO.setMid(mid);
        return orderService.settlement(settlementDTO);
    }

    @ApiOperation(value = "撤销退款申请", notes = "这里的id，传 退款对象的id")
    @PostMapping("/cancelRefund")
    public R<Boolean> cancelRefund(@RequestBody IdQueryDTO moneyReturnId) {
        Long mid = BaseContextHandler.getUserId();
        return orderService.cancelRefund(mid, moneyReturnId.getId());
    }

    @ApiOperation(value = "撤销退货申请", notes = "这里的id，传 退货对象的id")
    @PostMapping("/cancelReturnGoods")
    public R<Boolean> cancelReturnGoods(@RequestBody IdQueryDTO goodsReturnId) {
        Long mid = BaseContextHandler.getUserId();
        return orderService.cancelReturnGoods(mid, goodsReturnId.getId());
    }

    @ApiOperation(value = "取消订单", notes = "这里的id，传 订单的id")
    @PostMapping("/cancelOrder")
    public R<Boolean> cancelOrder(@RequestBody IdQueryDTO orderId) {
        Long mid = BaseContextHandler.getUserId();
        return orderService.cancelOrder(mid, orderId.getId());
    }

    @ApiOperation(value = "确认收货", notes = "这里的id，传 订单的id")
    @PostMapping("/confirmReceiveGooods")
    public R<Boolean> confirmReceiveGooods(@RequestBody IdQueryDTO orderId) {
        Long mid = BaseContextHandler.getUserId();
        return orderService.confirmReceiveGooods(mid, orderId.getId());
    }

    @ApiOperation(value = "查看订单详情", notes = "这里的id，传 订单的id")
    @PostMapping("/getOrderDetail")
    public R<OrderDetailVO> getOrderDetail(@RequestBody IdQueryDTO orderId) {
        Long mid = BaseContextHandler.getUserId();
        return orderService.getOrderDetail(mid, orderId.getId());
    }

    @ApiOperation(value = "售后列表")
    @PostMapping("/findReturnList")
    public R<ReturnListVO> findReturnList(@RequestBody PageQueryDTO pageQueryDTO) {
        Long mid = BaseContextHandler.getUserId();
        pageQueryDTO.setMid(mid);
        return orderService.findReturnList(pageQueryDTO);
    }

    @ApiOperation(value = "查询某个售后单的详情")
    @GetMapping("/getReturnDetail/{returnType}/{returnCode}")
    public R<ReturnDetailVO> getReturnDetail(@PathVariable("returnType") Integer returnType, @PathVariable("returnCode") String returnCode) {
        return orderService.getReturnDetail(returnType, returnCode);
    }

    @ApiOperation(value = "删除订单")
    @PostMapping("/delOrder")
    public R<Boolean> delOrder(@RequestBody IdQueryDTO idQueryDTO) {
        Long mid = BaseContextHandler.getUserId();
        return orderService.delOrder(mid, idQueryDTO.getId());
    }

    @ApiOperation(value = "删除售后订单")
    @PostMapping("/delReturnOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "退款单或者退货单的id"),
            @ApiImplicitParam(name = "type", value = "类型 1-退款单 2-退货单")
    })
    public R<Boolean> delReturnOrder(@RequestBody IdTypeQueryDTO idTypeQueryDTO) {
        Long mid = BaseContextHandler.getUserId();
        return orderService.delReturnOrder(mid, idTypeQueryDTO);
    }

    @ApiOperation("查询售后理由")
    @GetMapping("/getReasonEnums")
    public R<Map<String, Object>> getReasonEnums() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("returnMoneyReasonList", ReturnMoneyReasonEnum.generateList());
        map.put("returnGoodsReasonList", ReturnGoodsReasonEnum.generateList());
        return R.success(map);
    }

    @ApiOperation("查询物流轨迹")
    @GetMapping("/getShippingTrace/{shipperCode}/{logisticCode}")
    public R<ShippingDTO> getShippingTrace(@PathVariable("shipperCode") String shipperCode, @PathVariable("logisticCode") String logisticCode) {
        return R.success(orderService.getShippingTrace(shipperCode, logisticCode));
    }

    @ApiOperation(value = "下单支付回调", notes = "开发模拟回调，线上需要注释")
    @PostMapping("payOrderCallback")
    public R<String> payOrderCallback(@RequestBody PayOrderCallbackDTO payOrderCallbackDTO) {
        return orderService.payOrderCallBack(payOrderCallbackDTO);
    }



}
